
#import "RNSharePreference.h"

@implementation RNSharePreference

- (dispatch_queue_t)methodQueue
{
    return dispatch_get_main_queue();
}
RCT_EXPORT_MODULE()
RCT_REMAP_METHOD(saveValue,
                 saveKey:(NSString*)key
                 saveValue:(NSString*)value
                 resolve:(RCTPromiseResolveBlock)resolve
                 reject:(RCTPromiseRejectBlock)reject){
  
  if(key==nil || value==nil ||key.length==0 || value.length==0){
    reject(@"-1",@"key或value传入为空",nil);
    return;
  }
  [[NSUserDefaults standardUserDefaults] setValue:value forKey:key];
  [[NSUserDefaults standardUserDefaults] synchronize];
  resolve(@(true));
}

RCT_REMAP_METHOD(getValue,
                 getKey:(NSString*)key
                 resolve:(RCTPromiseResolveBlock)resolve
                 reject:(RCTPromiseRejectBlock)reject){
  
  if(key==nil || key.length==0){
    reject(@"-1",@"key传入为空",nil);
    return;
  }
  NSString* value = [[NSUserDefaults standardUserDefaults] objectForKey:key];
  resolve(value);
  
}

RCT_REMAP_METHOD(remove,
                 deleKey:(NSString*)key
                 resolve:(RCTPromiseResolveBlock)resolve
                 reject:(RCTPromiseRejectBlock)reject){
  
  if(key==nil || key.length==0){
    reject(@"-1",@"key传入为空",nil);
    return;
  }
  [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
  resolve(@(true));
}

@end
  
